package com.caigou.modules.forum.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.modules.forum.R

/**
 * @author #Suyghur.
 * Created on 2021/12/25
 */
class PacketOpenDialog(context: Context, private val headImg: String, private val nickName: String, private val onAnimEnd: () -> Unit) :
    Dialog(context) {

    private lateinit var ivAvatar: ImageView
    private lateinit var tvNickName: TextView

    private lateinit var ivClose: ImageView
    private lateinit var ivOpen: ImageView


    private val resIds = intArrayOf(
        R.drawable.img_forum_packet_open1,
        R.drawable.img_forum_packet_open2,
        R.drawable.img_forum_packet_open3,
        R.drawable.img_forum_packet_open4,
        R.drawable.img_forum_packet_open5,
        R.drawable.img_forum_packet_open6,
        R.drawable.img_forum_packet_open7,
        R.drawable.img_forum_packet_open8,
        R.drawable.img_forum_packet_open9,
        R.drawable.img_forum_packet_open10,
        R.drawable.img_forum_packet_open11,
    )

    private var frameAnimation: FrameAnimation? = null

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
    }

    private fun initView() {
        setContentView(R.layout.dialog_packet_open)

        ivAvatar = findViewById(R.id.packet_iv_avatar)
        if (TextUtils.isEmpty(headImg)) {
            ivAvatar.setImageResource(R.drawable.img_comm_avatar)
        } else {
            ImageLoader.loadCircle(context, headImg, ivAvatar)
        }

        ivClose = findViewById(R.id.packet_iv_close)
        ivClose.setOnClickListener {
            dismiss()
        }

        tvNickName = findViewById(R.id.packet_tv_nickname)
        tvNickName.text = nickName

        ivOpen = findViewById(R.id.packet_iv_open)
        ivOpen.setOnClickListener {
            startAnim()
        }
    }

    private fun startAnim() {
        frameAnimation = FrameAnimation(ivOpen, resIds, 125, false)
        frameAnimation?.setAnimationListener(object : FrameAnimation.AnimationListener {
            override fun onAnimationStart() {

            }

            override fun onAnimationEnd() {
                dismiss()
                onAnimEnd()
            }

            override fun onAnimationRepeat() {

            }

            override fun onAnimationPause() {
                ivOpen.setBackgroundResource(R.drawable.img_forum_packet_open1)
            }
        })
    }

    private fun stopAnim() {
        frameAnimation?.apply {
            release()
            frameAnimation = null
        }
    }

    override fun dismiss() {
        super.dismiss()
        stopAnim()
    }
}